package controller.permainan;

import helper.*;
import model.players.*;
import model.board.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class AI_controller_Lisa implements Runnable
{
    //ATTR
    public Permainan2 P;
    public Player aiplyr;
    public Kotak current_kotak;
    public Dadu_animator dadu_animator;
    static int counter_rolldice = 0;

    //CONS
    public AI_controller_Lisa (Permainan2 P)
    {
        this.P = P;
        aiplyr = P.G.current_plyr();
        current_kotak = P.G.current_kotak();
    }

    @SuppressWarnings("static-access")
    public void run() 
    {
        try
        {
            P.act_button.setEnabled(false);
            Thread.sleep(2000);
            P.act_button.setEnabled(true);
            if(aiplyr.masuk_penjara) //kondisi tidak masuk penjara
            {
                while(counter_rolldice<=2)
                    AI_AcakPenjara();
                if(aiplyr.getUang()>8000)
                {
                    P.HistoryWrite("You have paid IDR 5000 to exit from jail");
                    P.G.current_plyr().bayar(5000);
                    P.G.current_plyr().masuk_penjara = false;
                    P.aset_attr_panel.setVisible(false);
                }
            }
            else
            {
                do
                {
                    AI_Acak();
                    Thread.sleep(dadu_animator.dadu_time + 2000);
                    AI_CekKotak();
                    P.AssignAsThisPlyr(P.G.urutan_current_plyr);
                    Thread.sleep(2000);
                }
                while(aiplyr.dadu1 == aiplyr.dadu2);
                AI_Ganti();
            }
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(AI_controller_Lisa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    @SuppressWarnings("static-access")
    public void AI_Acak()
    {
        /*sebelum acak pasti ngecek dulu hunger nya dibawah 14 ato ngga supaya bisa jalan..
         14 tu soalnya kalo dia makan nambahnya cmn 5..
         jadi kalo makan jadi 19-->ga kelebihan ga kekurangan..
        */
        if(P.G.current_plyr().getHunger() <= 14)
            AI_Makan();
        if(P.G.current_plyr().getFun() <= 10)
            AI_Rekreasi();

        aiplyr.acak();
        if (P.G.current_plyr().getHunger() <= (aiplyr.dadu1 + aiplyr.dadu2))
        {
            //ga boleh maju dong
            aiplyr.acak();
            //KONDISI : HUNGER TIDAK MENCUKUPO
            if (P.G.current_plyr().getHunger() <= (aiplyr.dadu1 + aiplyr.dadu2))
            {
                P.HistoryWrite("You can't move. Eat or do vacation first.");
                AI_Makan();
            }
        }
        //KONDISI : HUNGER MENCUKUPI
        else
        {
            dadu_animator = new Dadu_animator(P, aiplyr.dadu1, aiplyr.dadu2);
            Thread t = new Thread(dadu_animator);
            t.start();
            try {
            t.join();
            } catch (InterruptedException ex) {
            Logger.getLogger(AI_controller_Lisa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @SuppressWarnings("static-access")
    public void AI_CekKotak()
    {
           switch (P.G.current_kotak().getTipe())
                {
                case 2: //LP
                    LokasiPariwisata LP = (LokasiPariwisata) P.G.current_kotak();
                    if ( (P.G.current_kotak().getOwner() != null))
                    {
                        //KONDISI : KOTAK LOKASI PARIWISATA MILIK ORANG LAIN DAN DIA PUNYA SATU KOMPLEKS
                        if (!P.G.current_kotak().getOwner().equals(P.G.current_plyr()) && (SearchEngine.DoYouHaveThisComplex(P.G.B, P.G.current_plyr().getPosisi(), P.G.current_kotak().getOwner() )))
                        {
                            P.G.current_plyr().sudah_bayar = false;
                            //HARGA SEWA KALI 2
                            if (aiplyr.bayar(2*P.G.current_kotak().getHargasewa()))
                            {
                                P.G.current_kotak().getOwner().addUang(P.G.current_kotak().getHargasewa());
                                P.HistoryWrite(aiplyr.getNama()+" pay Rp."+P.G.current_kotak().getHargasewa()+" rent to " +P.G.current_kotak().getOwner().getNama());
                            }
                            else
                            {
                            //TODO : bangkrut? jual dulu si harusnya tapi tetap bingung
                            }
                        }
                        //KONDISI : KOTAK LOKASI PARIWISATA MILIK ORANG LAIN DAN BELUM PUNYA SATU KOMPLEKS
                        if (!P.G.current_kotak().getOwner().equals(P.G.current_plyr()))
                        {
                            P.G.current_plyr().sudah_bayar = false;
                            if (aiplyr.bayar(P.G.current_kotak().getHargasewa()))
                            {
                                P.G.current_kotak().getOwner().addUang(P.G.current_kotak().getHargasewa());
                                P.HistoryWrite(aiplyr.getNama()+" pay Rp."+P.G.current_kotak().getHargasewa()+" rent to " +P.G.current_kotak().getOwner().getNama());
                            }
                            else
                            {
                            //TODO : bangkrut? jual dulu si harusnya tapi tetap bingung
                            }
                        }
                        //KODISI : KOTAK LOKASI PARIWISATA MILIK SENDIRI
                        else if ( (P.G.current_kotak().getOwner().equals(P.G.current_plyr())))
                        {
                            if (SearchEngine.DoYouHaveThisComplex(P.G.B, P.G.current_plyr().getPosisi(), P.G.current_plyr() ))
                            {
                                if (LP.Build(P.G.current_plyr()))
                                {
                                    P.HistoryWrite (P.G.current_plyr().getNama()+" has build SOMETHING at "+LP.getNama());
                                }
                                
                            }
                        }
                    }
                    //KONDISI : BELUM ADA YANG PUNYA
                    else
                    {
                        if (aiplyr.cukup(P.G.current_kotak().getHargabeli()))
                        {
                            P.G.current_kotak().BeliAset(aiplyr);
                            P.HistoryWrite(aiplyr.getNama()+" buy "+P.G.current_kotak().getNama()+" for "+P.G.current_kotak().getHargabeli());
                            AI_CekKotak(); //siapa tau bisa beli villa
                        }
                    }
                    break;

                case 3: //DANA UMUM
                    P.kes.getMethodDana(P.counter_community, P.G);
                    P.counter_community++;
                    if(P.counter_community >= 5)
                        P.counter_community=0;
                    break;

                case 4: //airport
                    if ( (P.G.current_kotak().getOwner() != null))
                    {
                        //KONDISI : AIRPORT MILIK ORANG LAIN
                        if (!P.G.current_kotak().getOwner().equals(P.G.current_plyr()))
                        {
                            P.G.current_plyr().sudah_bayar = false;
                            if (aiplyr.bayar(current_kotak.getHargasewa()))
                            {
                                P.HistoryWrite(aiplyr.getUang()+" "+aiplyr.getNama()+" pay Rp."+P.G.current_kotak().getHargasewa()+" rent to " +P.G.current_kotak().getOwner().getNama());
                            }
                            else
                            {
                                //TODO : bangkrut?
                            }

                        }
                        //KONDISI : AIRPORT MILIK PEMAIN
                        else if ( (P.G.current_kotak().getOwner().equals(P.G.current_plyr())))
                        {
                               P.NotifWrite ("This airport is yours.");
                        }
                    }
                    else
                    {
                        if (aiplyr.cukup(current_kotak.getHargabeli()))
                        {
                            current_kotak.BeliAset(aiplyr);
                            P.HistoryWrite(aiplyr.getNama()+" buy "+current_kotak.getNama()+" for "+current_kotak.getHargabeli());
                        }
                    }
                    break;

                case 6: //masuk penjara
                    
                    //PEMAIN PINDAH KE KOTAK NO 9
                    P.G.current_plyr().setPosisi(9);
                    P.ShowPlyrAllPosition();
                    P.G.current_plyr().masuk_penjara = true ;
                    P.HistoryWrite(P.G.current_plyr().getNama()+" are in jail. Your turn is over.");
                    P.playerattr_button[P.G.urutan_current_plyr].setBackground(null);
                    P.DisableButtons();
                    P.G.urutan_current_plyr = P.G.Q.urutan_nextPlayer(P.G.urutan_current_plyr);
                    P.AssignAsThisPlyr(P.G.urutan_current_plyr);
                    P.ShowPlyrAttr(P.G.urutan_current_plyr);
                    P.act_button.setText("Roll Dice");
                    break;

                 case 8: //parkir bebas
                    break;

                case 9: //KESEMPATAN
                    P.kes.getMethodKes(P.counter_chance, P.G);
                    switch(P.counter_chance)
                    {
                        case 0 : P.ShowPlyrAllPosition();break;
                        case 2 : P.ShowPlyrAllPosition(); P.CekKotak(); break;
                        case 3 : P.ShowPlyrAllPosition(); P.CekKotak(); break;
                        case 4 : P.act_button.setText("Roll Dice");
                    }
                    P.counter_chance++;
                    if(P.counter_chance >= 5)
                        P.counter_chance = 0;
                    break;

                case 10: //reverse
                    P.puts("cek_kotak : reverse");
                    //KONDISI : URUTAN PEMAIN DIBALIK
                    P.G.Q.arah_maju = !P.G.Q.arah_maju;
                    P.NotifWrite("\tArah urutan pemain dibalik.");
                    break;

                case 11: //skip
                    P.puts("cek_kotak : skip");
                    //KONDISI : PEMAIN GILIRAN SELANJUTNYA DISKIP
                    P.HistoryWrite(P.G.current_plyr().getNama()+" has skipped "+P.G.Q.player[P.G.Q.urutan_nextPlayer(P.G.urutan_current_plyr)].getNama());
                    P.playerattr_button[P.G.urutan_current_plyr].setBackground(null);
                    P.DisableButtons();
                    P.G.skip_next = true;
                    break;
                   }

       //P.AssignAsThisPlyr(P.G.urutan_current_plyr);
    }

    public void AI_Ganti()
    {
        P.HistoryWrite(aiplyr.getNama() + " has just finished her turn.");
        P.playerattr_button[P.G.urutan_current_plyr].setBackground(null);
        P.SetDicesDisplayed(0, 0);
        P.DisableButtons();
        P.G.urutan_current_plyr = P.G.Q.urutan_nextPlayer(P.G.urutan_current_plyr);
        P.AssignAsThisPlyr(P.G.urutan_current_plyr);
        P.act_panel.setVisible(true);

        P.CheckBankrupt();
        P.act_button.setText("Roll Dice");
        if (!P.G.current_plyr().getHuman())
        {
            P.NotifWrite (P.G.current_plyr().getNama()+" bukan manusia loh. Jangan2 pocong.");
            System.out.println("bukan manusia loh. Jangan2 pocong.");
            Thread ai_thread = new Thread (this);
            ai_thread.start();
        }
    }

    public void AI_Makan()
    {
        LokasiPariwisata LP = (LokasiPariwisata) P.G.current_kotak();
       if (P.G.current_plyr().cukup(LP.getHarga_makanan()))
       {
           P.G.current_plyr().makan(LP.getIncrease_makanan());
           P.G.current_plyr().bayar(LP.getHarga_makanan());
           P.HistoryWrite (P.G.current_plyr().getNama()+" did "+LP.getMakanan()+" for "+LP.getHarga_makanan()+ " and had full added by "+LP.getIncrease_makanan());
       }
       else
           P.HistoryWrite("NFS! Not sufficient fund");
       P.ShowPlyrAttr(P.G.urutan_current_plyr);
    }

    public void AI_Rekreasi()
    {
       LokasiPariwisata LP = (LokasiPariwisata) P.G.current_kotak();
       if (P.G.current_plyr().cukup(LP.getHarga_rekreasi()))
       {
           P.G.current_plyr().bayar(LP.getHarga_rekreasi());
           P.G.current_plyr().rekreasi(LP.getIncrease_rekreasi());
           P.HistoryWrite (P.G.current_plyr().getNama()+" did "+LP.getRekreasi()+" for "+LP.getHarga_rekreasi()+ " and had fun added by "+LP.getIncrease_rekreasi());
       }
       else
           P.HistoryWrite("NFS! Not sufficient fund");
       P.ShowPlyrAttr(P.G.urutan_current_plyr);
    }

    public void AI_AcakPenjara()
    {
        P.HistoryWrite(P.G.current_plyr().getNama() + " "+Integer.toString(counter_rolldice)+"x rolled the dice.\n "+P.G.current_plyr().acak());
        int dadu1 = P.G.current_plyr().dadu1;
        int dadu2 = P.G.current_plyr().dadu2;

        if (dadu1==dadu2)       //dadu sama, boleh keluar
        {
            counter_rolldice = 0;
            P.HistoryWrite("Congratulation! You've got the same dice numbers!");
            P.G.current_plyr().masuk_penjara = false;
        }
        else if (dadu1!=dadu2)      //dadu beda
        {
            //dicek udah berapa kali ngacaknya
            if (counter_rolldice>=3) //udah 3 kali
            {
                counter_rolldice = 0;
                P.HistoryWrite(aiplyr.getNama()+"has failed to escape three times! ");
                AI_Ganti();
            }
            else
            {
                P.HistoryWrite("Dices : "+dadu1+" and "+dadu2+". "+aiplyr.getNama()+" failed to escape!");
            }
        }
    }

    //DRIVER
    public static void main (String[] args)
    {
        Permainan2 P = new Permainan2();
        P.G.Q.player[1].setHuman(false);
        P.G.Q.player[4].setHuman(false);
        Thread t = new Thread (new AI_controller_Lisa(P));
        t.start();

    }




}
